package com.file_.Buffered_zijieStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/*
 * 使用BufferedInputStream和BufferedOutputStream完成 二进制 文件的拷贝
 * 如图片这种二进制文件的拷贝
 * */
public class Demo1 {
    public static void main(String[] args) throws Exception {
        /*
         * 字节流可以操作二进制文件
         * 也可以操作文本文件
         * */

        String srcfilePath = "D:\\A_Test\\bufferCopy.JPG";
        String destfilePath = "D:\\A_Test\\图片\\bufferCopy.JPG";
        // 1、创建对象
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfilePath));
        bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destfilePath));
        byte[] buf = new byte[1024];
        int readLen = 0;

        // 2、循环读取，并写入
        // 返回-1， 表示读取完毕
        while ((readLen = bufferedInputStream.read(buf)) != -1) {
            bufferedOutputStream.write(buf, 0, readLen);
        }

        // 3、关闭处理流
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
    }
}
